home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Commodities / ColorSaver / src / colorfuncs.c next >
Encoding:
C/C++ Source or Header  |  1996-09-27  |  9.7 KB  |  358 lines

  1. #include <intuition/intuition.h>
  2. #include <libraries/gadtools.h>
  3. #include <clib/macros.h>
  4. #include <stdlib.h>
  5. #include <clib/gadtools_protos.h>
  6. #include <clib/graphics_protos.h>
  7.  
  8.  
  9. #include <stdio.h>
  10.  
  11. #include "protos.h"            /* Routine prototypes    */
  12. #include "gadgets.h"               /* GadToolsBox file     */
  13. #include "defs.h"
  14.  
  15. extern struct Screen     *Scr;            /* screen we open up on            */
  16. extern UBYTE            CSIsOpen;       /* window open?             */
  17. extern USHORT        ColorMode;    /* Color selection mode            */
  18. extern UWORD         RedVal;        /* RGB red value            */
  19. extern UWORD         GreenVal;    /* RGB green value            */
  20. extern UWORD         BlueVal;    /* RGB blue value            */
  21. extern UWORD            HueLevel;    /* HSV hue                 */
  22. extern UWORD         SatLevel;       /* HSV saturation            */
  23. extern UWORD         ValLevel;       /* HSV value            */
  24. extern SHORT        CurrentColor;    /* Currently selected color     */
  25. extern ULONG        NumColors;    /* # of colors in palette       */
  26.  
  27. /*=============================================================================
  28.  * Set the slider props to reflect the proper position for the supplied color
  29.  * number.  If the window is not open (I.E. called from ARexx) then disregard
  30.  *===========================================================================*/
  31.  
  32. void SetProps(SHORT num)
  33. {
  34.  
  35.  if(!CSIsOpen) return;  /* If called from ARexx and window hid */
  36.  
  37.  GetColors(num);
  38.  if(ColorMode == RGB_COLORMODE)
  39.   {
  40.    GT_SetGadgetAttrs(GAD(GD_RED_GAD),  ColorSaverWnd,NULL,GTSL_Level,(WORD)RedVal,TAG_END);
  41.    GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Level,(WORD)GreenVal,TAG_END);
  42.    GT_SetGadgetAttrs(GAD(GD_BLUE_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)BlueVal,TAG_END);
  43.   }
  44.  else
  45.   {
  46.    CalcHSV(RedVal,GreenVal,BlueVal);
  47.    GT_SetGadgetAttrs(GAD(GD_RED_GAD),  ColorSaverWnd,NULL,GTSL_Level,(WORD)HueLevel,TAG_END);
  48.    GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Level,(WORD)SatLevel,TAG_END);
  49.    GT_SetGadgetAttrs(GAD(GD_BLUE_GAD), ColorSaverWnd,NULL,GTSL_Level,(WORD)ValLevel,TAG_END);
  50.   }
  51. }
  52.  
  53.  
  54. /*=============================================================================
  55.  * Set the prop size and identifiers to either RGB or HSV 
  56.  *===========================================================================*/
  57.  
  58. void SetColorMode(USHORT mode)
  59. {
  60.  
  61.  if(!CSIsOpen) return;  /* If called from ARexx and window is hid */
  62.  
  63.  if(mode == RGB_COLORMODE)
  64.   {
  65.  
  66.    GT_SetGadgetAttrs(GAD(GD_RGBHSV_GAD),ColorSaverWnd,NULL,GTMX_Active,(UWORD)0,TAG_END);
  67.  
  68.    GT_SetGadgetAttrs(GAD(GD_RGBR_GAD),ColorSaverWnd,NULL,GTTX_Text,"R",TAG_END);
  69.    GT_SetGadgetAttrs(GAD(GD_RGBG_GAD),ColorSaverWnd,NULL,GTTX_Text,"G",TAG_END);
  70.    GT_SetGadgetAttrs(GAD(GD_RGBB_GAD),ColorSaverWnd,NULL,GTTX_Text,"B",TAG_END);
  71.  
  72.    GT_SetGadgetAttrs(GAD(GD_RED_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
  73.    GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
  74.    GT_SetGadgetAttrs(GAD(GD_BLUE_GAD),ColorSaverWnd,NULL,GTSL_Max,15,TAG_END);
  75.  
  76.   }
  77.  else
  78.   {
  79.  
  80.    GT_SetGadgetAttrs(GAD(GD_RGBHSV_GAD),ColorSaverWnd,NULL,GTMX_Active,(UWORD)1,TAG_END);
  81.  
  82.    GT_SetGadgetAttrs(GAD(GD_RGBR_GAD),ColorSaverWnd,NULL,GTTX_Text,"H",TAG_END);
  83.    GT_SetGadgetAttrs(GAD(GD_RGBG_GAD),ColorSaverWnd,NULL,GTTX_Text,"S",TAG_END);
  84.    GT_SetGadgetAttrs(GAD(GD_RGBB_GAD),ColorSaverWnd,NULL,GTTX_Text,"V",TAG_END);
  85.  
  86.    GT_SetGadgetAttrs(GAD(GD_RED_GAD),ColorSaverWnd,NULL,GTSL_Max,359,TAG_END);
  87.    GT_SetGadgetAttrs(GAD(GD_GREEN_GAD),ColorSaverWnd,NULL,GTSL_Max,100,TAG_END);
  88.    GT_SetGadgetAttrs(GAD(GD_BLUE_GAD),ColorSaverWnd,NULL,GTSL_Max,100,TAG_END);
  89.   }
  90. }
  91.  
  92. /*=============================================================================
  93.  * Get the current color bits for the specified color number 
  94.  *===========================================================================*/
  95.  
  96. void GetColors(SHORT num)
  97. {
  98.  UWORD col;
  99.  
  100.  col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)num);
  101.  RedVal  = (col >> 8) & 0x0F;
  102.  GreenVal= (col >> 4) & 0x0F;
  103.  BlueVal = (col     ) & 0x0F;
  104.  
  105. }
  106.  
  107. /*=============================================================================
  108.  * Set the current palette color to the specified color number
  109.  *===========================================================================*/
  110.  
  111. void SetPaletteColor(SHORT color)
  112. {
  113.  
  114.    CurrentColor = COLORValid(color,NumColors,CurrentColor);
  115.  
  116.    if(!CSIsOpen) return;  /* If called from ARexx and window is hid */
  117.  
  118.    GT_SetGadgetAttrs(GAD(GD_PALETTE_GAD), ColorSaverWnd,NULL,GTPA_Color,
  119.     color,TAG_END);   
  120. }
  121.  
  122. /*=============================================================================
  123.  * Shift the entire color palette one color to the left.  I.E, for a 4-color 
  124.  * palette, COLOR 0 becomes COLOR 3, COLOR 2 becomes COLOR 1, etc...
  125.  *===========================================================================*/
  126.  
  127. void ShiftColorsLeft( void )
  128. {
  129.    register SHORT i,j;
  130.  
  131.    UWORD firstred; 
  132.    UWORD firstgreen;
  133.    UWORD firstblue;
  134.  
  135.    GetColors(0);
  136.  
  137.    firstred = RedVal; 
  138.    firstgreen = GreenVal;
  139.    firstblue = BlueVal;
  140.    
  141.    for (i = 0, j=1; i < NumColors; i++,j++)   
  142.    {
  143.      if(i == NumColors-1)  /* last color to be set    */
  144.       {
  145.            RedVal = firstred;
  146.       GreenVal = firstgreen;
  147.     BlueVal = firstblue;
  148.       }
  149.      else
  150.         GetColors(j);
  151.  
  152.      SetRGB4(&Scr->ViewPort,i,RedVal,GreenVal,BlueVal);        
  153.  
  154.    }
  155. }
  156.  
  157. /*=============================================================================
  158.  * Shift the entire color palette one color to the right.  I.E, for a 4-color 
  159.  * palette, COLOR 0 becomes COLOR 1, COLOR 3 becomes COLOR 0, etc...
  160.  *===========================================================================*/
  161. void ShiftColorsRight( void )
  162. {
  163.    register SHORT i,j;
  164.  
  165.    UWORD firstred; 
  166.    UWORD firstgreen;
  167.    UWORD firstblue;
  168.  
  169.    GetColors(NumColors-1);
  170.  
  171.    firstred = RedVal; 
  172.    firstgreen = GreenVal;
  173.    firstblue = BlueVal;
  174.    
  175.    for (i = NumColors-1, j=NumColors-2; i > -1; --i,j--)   
  176.    {
  177.      if(i == 0)  /* last color to be set    */
  178.       {
  179.            RedVal = firstred;
  180.       GreenVal = firstgreen;
  181.     BlueVal = firstblue;
  182.       }
  183.      else
  184.         GetColors(j);
  185.  
  186.      SetRGB4(&Scr->ViewPort,i,RedVal,GreenVal,BlueVal);        
  187.  
  188.    }
  189. }
  190.  
  191. /*=============================================================================
  192.  * Spread the colors over a specified range
  193.  *===========================================================================*/
  194.  
  195. void ColorRange( SHORT first, SHORT last )
  196. {
  197.    register SHORT i;
  198.    LONG  whole, redfraction, greenfraction, bluefraction;
  199.    UWORD col;
  200.    UWORD firstred, firstgreen, firstblue;
  201.    UWORD lastred, lastgreen, lastblue;
  202.    UWORD workred, workgreen, workblue;
  203.  
  204.    if (first > last) {
  205.       i = first;
  206.       first = last;
  207.       last = i;
  208.    }
  209.  
  210.    /* I need to see a spread of at least two, where there's at least one
  211.     * spot between the endpoints, else there's no work to do so I
  212.     * might as well just return now.
  213.     */
  214.  
  215.    if (first >= last - 1)
  216.       return;
  217.  
  218.    col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)first);
  219.  
  220.    firstred   = (col >> 8) & 0x0F;
  221.    firstgreen = (col >> 4) & 0x0F;
  222.    firstblue  = (col     ) & 0x0F;
  223.  
  224.    col=GetRGB4(Scr->ViewPort.ColorMap,(LONG)last);
  225.  
  226.    lastred   = (col >> 8) & 0x0F;
  227.    lastgreen = (col >> 4) & 0x0F;
  228.    lastblue  = (col     ) & 0x0F;
  229.  
  230.    whole = ((LONG) (lastred - firstred)) << 16;
  231.    redfraction = whole / (last - first);
  232.    whole = ((LONG)(lastgreen - firstgreen)) << 16;
  233.    greenfraction = whole / (last - first);
  234.    whole = ((LONG)(lastblue - firstblue)) << 16;
  235.    bluefraction = whole / (last - first);
  236.  
  237.    for (i = first + 1; i < last; i++)
  238.       {
  239.       lastred = (redfraction * (i - first) + 0x8000) >> 16;
  240.       workred = firstred + lastred;
  241.       lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
  242.       workgreen = firstgreen + lastgreen;
  243.       lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
  244.       workblue = firstblue + lastblue;
  245.       SetRGB4(&Scr->ViewPort,i,workred,workgreen,workblue);
  246.       }
  247. }
  248.  
  249.  
  250. #define MAX3(a,b,c) MAX(a,MAX(b,c))
  251. #define MIN3(a,b,c) MIN(a,MIN(b,c))
  252.  
  253. /*=============================================================================
  254.  * The following two routines are slightly modified versions of routines
  255.  * heisted from "HSV" by Frank Ederveen (FF791).  Thanks for the good 
  256.  * explanation of how HSV works, Frank.  I really hadn't much of an idea,
  257.  * and never really thought of adding HSV to ColorSaver until I saw your
  258.  * program....
  259.  *===========================================================================*/
  260.  
  261. void CalcHSV ( UWORD red, UWORD green, UWORD blue )
  262. {
  263.   float hue, sat, delta, r, g, b, max, min;
  264.  
  265.   r = ((float) red  ) / 15;
  266.   g = ((float) green) / 15;
  267.   b = ((float) blue ) / 15;
  268.  
  269.   max = MAX3(r,g,b);
  270.   min = MIN3(r,g,b);
  271.  
  272.   ValLevel = 100 * max;
  273.  
  274.   if (max != 0)
  275.     sat = (max - min) / max;
  276.   else
  277.     sat = 0;
  278.  
  279.   if (sat == 0)
  280.     hue = 0;
  281.   else
  282.     {
  283.       delta = max - min;
  284.  
  285.       if (r == max)
  286.     hue = (g - b) / delta;
  287.       else if (g == max)
  288.     hue = 2 + (b - r) / delta;
  289.       else
  290.     hue = 4 + (r - g) / delta;
  291.  
  292.       hue *= 60;
  293.  
  294.       if (hue < 0)
  295.     hue += 360;
  296.     }
  297.   HueLevel = hue;
  298.   SatLevel = 100 * sat;
  299.  
  300. }
  301.  
  302. void CalcRGB( void )
  303. {
  304.   float hue, sat, val, r, g, b, p1, p2, p3;
  305.   int i = 0;
  306.   float f = 0;
  307.  
  308.   hue = (float) HueLevel / 60;
  309.   val = (float) ValLevel / 100;
  310.   sat = (float) SatLevel / 100;
  311.  
  312.   while ((f = hue - i) > 1)
  313.     i++;
  314.  
  315.   p1 = val * (1 - sat);
  316.   p2 = val * (1 - (sat * f));
  317.   p3 = val * (1 - (sat * (1 - f)));
  318.  
  319.   switch (i)
  320.     {
  321.     case 0:
  322.       r = val;
  323.       g = p3;
  324.       b = p1;
  325.       break;
  326.     case 1:
  327.       r = p2;
  328.       g = val;
  329.       b = p1;
  330.       break;
  331.     case 2:
  332.       r = p1;
  333.       g = val;
  334.       b = p3;
  335.       break;
  336.     case 3:
  337.       r = p1;
  338.       g = p2;
  339.       b = val;
  340.       break;
  341.     case 4:
  342.       r = p3;
  343.       g = p1;
  344.       b = val;
  345.       break;
  346.     case 5:
  347.       r = val;
  348.       g = p1;
  349.       b = p2;
  350.       break;
  351.     }
  352.  
  353.   RedVal   = (UWORD) (r * 15);
  354.   GreenVal = (UWORD) (g * 15);
  355.   BlueVal  = (UWORD) (b * 15);
  356.  
  357. }
  358.